Imports QuizBase

Public Class Quiz
    Implements IQuizEngine


    Private _cardHistory As List(Of QuizEntities.QuizCard)
    Public Property CardHistory() As List(Of QuizEntities.QuizCard)
        Get
            Return _cardHistory
        End Get
        Set(ByVal value As List(Of QuizEntities.QuizCard))
            _cardHistory = value
        End Set
    End Property



    Private _xmlHandler As XmlHandler
    Public Property XmlHandler() As XmlHandler
        Get
            Return _xmlHandler
        End Get
        Set(ByVal value As XmlHandler)
            _xmlHandler = value
        End Set
    End Property

#Region "private"
    Private QuestionCount As Integer
    Private CurrentQuestion As Integer
    Public Score As Integer
#End Region

    Public Sub New(ByVal QuizUniqueID As String)
        CardHistory = New List(Of QuizEntities.QuizCard)
        XmlHandler = New XmlHandler(TranslateQuizUniqueIdToFileName(QuizUniqueID))
    End Sub


    Private Function TranslateQuizUniqueIdToFileName(ByVal QuizUniqueID As String) As String
        'QuestionCount = 5
        'Return "C:\inetpub\vhosts\framedphotos.in\httpdocs\App_Data\QAs.xml"

        QuestionCount = 5
        Return "C:\Documents and Settings\varun\My Documents\Visual Studio 2008\Projects\QuizEngine_version2\QuizEngine\QAs.xml"

    End Function



    ''' <summary>
    ''' Search card in card history.
    ''' </summary>
    ''' <param name="currentCardNumber"></param>
    ''' <returns>Returns card if founf</returns>
    ''' <remarks></remarks>
    Private Function SearchCard(ByVal currentCardNumber As Integer) As QuizEntities.QuizCard
        Dim tempCard As New QuizEntities.QuizCard()
        For Each card As QuizEntities.QuizCard In CardHistory
            If card.Status.CurrentQuestion = currentCardNumber Then
                tempCard = card
                Return tempCard
            End If
        Next
        Return Nothing
    End Function


    ''' <summary>
    ''' Search card in Card History list.
    ''' </summary>
    ''' <param name="currentCardNumber"></param>
    ''' <returns>true if it finds a card</returns>
    ''' <remarks></remarks>
    Private Function Exists(ByVal currentCardNumber As Integer) As Boolean
        For Each card As QuizEntities.QuizCard In CardHistory
            If card.Status.CurrentQuestion = currentCardNumber Then
                Return True
            End If
        Next
        Return False
    End Function



    'Public Function GetPrevQuestion(ByVal quizCard As QuizEntities.QuizCard) As QuizEntities.QuizCard Implements IQuizEngine.GetPrevQuestion
    '    Dim currentCardNumber As Integer = 1
    '    Dim searchedCard As QuizEntities.QuizCard
    '    Dim prevCard As QuizEntities.QuizCard
    '    If quizCard IsNot Nothing Then
    '        currentCardNumber = quizCard.Status.CurrentQuestion
    '        currentCardNumber = currentCardNumber - 1
    '        'check if card is first card.
    '        If currentCardNumber < 1 Then
    '            prevCard = GetNewQuestionCard()
    '            prevCard.Status.IsFirstQuestion = True
    '            Return prevCard
    '        End If
    '        ' check if card is present in card history.
    '        searchedCard = SearchCard(currentCardNumber)
    '        If searchedCard IsNot Nothing Then
    '            Return searchedCard
    '        Else 'nothing should be returned from card history.
    '            Throw New Exception("Error ID:001 No Previous Question Found")
    '        End If
    '    Else
    '        'If quiz card is coming as nothing, it implies it is first card, or user clicked on previous question on start of quiz.
    '        prevCard = GetNewQuestionCard()
    '        prevCard.Status.IsFirstQuestion = True
    '        Return prevCard
    '    End If
    'End Function


    'Public Sub SubmitQuestionCardToCache(ByVal quizCard As QuizEntities.QuizCard) Implements IQuizEngine.SubmitQuestionCardToCache

    '    Dim currentCardNumber As Integer

    '    If quizCard IsNot Nothing Then
    '        currentCardNumber = quizCard.Status.CurrentQuestion
    '        'Score = quizCard.Status.Score
    '        ' currentCardNumber = currentCardNumber + 1
    '        If Not Exists(quizCard.Status.CurrentQuestion) Then CardHistory.Add(DeepCopy(quizCard))
    '        'Else
    '        '    quizCard = GetNewQuestionCard()
    '    End If
    'End Sub


    Public Function GetNextQuestion(ByVal quizCard As QuizEntities.QuizCard) As QuizEntities.QuizCard Implements IQuizEngine.GetNextQuestion
        Dim currentCardNumber As Integer = 1
        Dim searchedCard As QuizEntities.QuizCard
        Dim lastCard As QuizEntities.QuizCard
        Dim nextCard As QuizEntities.QuizCard

        If quizCard IsNot Nothing Then
            Score = quizCard.Status.CorrectQuestionCount
            currentCardNumber = quizCard.Status.CurrentQuestion
            currentCardNumber = currentCardNumber + 1
        End If
        'check if card is present in card history.
        searchedCard = SearchCard(currentCardNumber)
        If searchedCard IsNot Nothing Then
            Return DeepCopy(searchedCard)
        End If
        'check if current question card second last question.
        If currentCardNumber > QuestionCount Then
            lastCard = DeepCopy(quizCard)
            lastCard.Status.IsLastQuestion = True
            Return lastCard
        Else
            'All checks failed, so return next question from xml file.
            nextCard = GetNewQuestionCard()
            nextCard.Status.CurrentQuestion = currentCardNumber
            nextCard.Status.CorrectQuestionCount = Score
            nextCard.QuizUniqueId = quizCard.QuizUniqueId
            nextCard.QuestionCount = QuestionCount
            nextCard.QusestionCard = XmlHandler.GetQuestionById(currentCardNumber.ToString())
            Return nextCard
        End If

    End Function

    Private Function GetNewQuestionCard() As QuizEntities.QuizCard
        Dim questionCard As New QuizEntities.QuizCard()
        questionCard.QuestionCount = QuestionCount
        Return questionCard
    End Function

    Private Function DeepCopy(ByVal source As QuizEntities.QuizCard) As QuizEntities.QuizCard
        Dim questionCard As New QuizEntities.QuizCard()
        questionCard.QuestionCount = source.QuestionCount
        questionCard.QuizUniqueId = source.QuizUniqueId
        questionCard.QusestionCard.AdditionalInfo = source.QusestionCard.AdditionalInfo
        questionCard.QusestionCard.CorrectAnswer = source.QusestionCard.CorrectAnswer
        questionCard.QusestionCard.ExtraInfo = source.QusestionCard.ExtraInfo
        questionCard.QusestionCard.OptionFive = source.QusestionCard.OptionFive
        questionCard.QusestionCard.OptionFour = source.QusestionCard.OptionFour
        questionCard.QusestionCard.OptionOne = source.QusestionCard.OptionOne
        questionCard.QusestionCard.OptionSix = source.QusestionCard.OptionSix
        questionCard.QusestionCard.OptionThree = source.QusestionCard.OptionThree
        questionCard.QusestionCard.OptionTwo = source.QusestionCard.OptionTwo
        questionCard.QusestionCard.Ques = source.QusestionCard.Ques
        questionCard.QusestionCard.QuesCategory = source.QusestionCard.QuesCategory
        questionCard.QusestionCard.QuesHint = source.QusestionCard.QuesHint
        questionCard.QusestionCard.QuesId = source.QusestionCard.QuesId
        questionCard.QusestionCard.QuizCategory = source.QusestionCard.QuizCategory
        questionCard.QusestionCard.QuizId = source.QusestionCard.QuizId
        questionCard.QusestionCard.TopicCategory = source.QusestionCard.TopicCategory

        questionCard.Status.AlertUser = source.Status.AlertUser
        questionCard.Status.CurrentQuestion = source.Status.CurrentQuestion
        questionCard.Status.HowManyAnsweredThisCorrectly = source.Status.HowManyAnsweredThisCorrectly
        questionCard.Status.IsAnswered = source.Status.IsAnswered
        questionCard.Status.IsCorrectAnswered = source.Status.IsCorrectAnswered
        questionCard.Status.IsFirstQuestion = source.Status.IsFirstQuestion
        questionCard.Status.IsLastQuestion = source.Status.IsLastQuestion
        questionCard.Status.CorrectQuestionCount = source.Status.CorrectQuestionCount


        Return questionCard
    End Function



    Public Function ValidateAnswer(ByVal currentCard As QuizEntities.QuizCard) As String Implements IQuizEngine.ValidateAnswer
        Return XmlHandler.GetQuestionById(currentCard.Status.CurrentQuestion).CorrectAnswer
    End Function


End Class
